package org.springblade.common.report;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import io.reactivex.rxjava3.core.Single;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.digitalization.entity.A01Entity;
import org.springblade.modules.digitalization.service.IA01Service;
import org.springblade.modules.digitalization.vo.UmsUserErInfoVo;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("ErCodeDs")
@Slf4j
@AllArgsConstructor
public class ErCodeDs {

	final private IA01Service a01Service;
	private List<A01Entity> tmpBu=Lists.newArrayList();
	public List<ErCodeEntity> loadReportData(String dsName, String datasetName, Map<String,Object> parameters){

		log.info(datasetName+dsName+parameters);
		int mod=6;
		List<ErCodeEntity> objects1=Lists.newArrayList();
		try{
			String codetype = (String) parameters.get("codetype");
			List<A01Entity> list=null;
			ErCodeEntity tmpEntity=null;
			if(Func.isBlank(codetype)||codetype.equals("today")){
				DateTime dateTime = DateUtil.beginOfDay(new Date());
				list = a01Service.list(Wrappers.<A01Entity>lambdaQuery().gt(A01Entity::getCreateTime, dateTime).eq(A01Entity::getIsDeleted, 0));

			}else if(codetype.equals("bu")){
				list=tmpBu;
			}
			int bu=(mod-list.size()%mod)%mod;
			if(list.size()==0){
				bu=6;
			}
			for (int j = 0; j < bu; j++) {
				A01Entity a01Entity = new A01Entity();
				a01Entity.setA0000("-");
				a01Entity.setA0184("-");
				a01Entity.setA0101("空");
				list.add(a01Entity);
			}
			for (int i = 0; i < list.size(); i++) {
				int tmpMod=i%mod;
				if(tmpMod==0){
					tmpEntity=new ErCodeEntity();
					tmpEntity.setA0000_7("aaa");
					tmpEntity.setA0000_7("bbb");
					tmpEntity.setA0000_7("ddd");
					objects1.add(tmpEntity);
				}
				A01Entity umsUserErInfoVo = list.get(i);
				switch (tmpMod){
					case 0:
						tmpEntity.setA0000_1(umsUserErInfoVo.getA0000());
						tmpEntity.setA0101_1(umsUserErInfoVo.getA0101());
						tmpEntity.setA0184_1(umsUserErInfoVo.getA0184());
						break;
					case 1:
						tmpEntity.setA0000_2(umsUserErInfoVo.getA0000());
						tmpEntity.setA0101_2(umsUserErInfoVo.getA0101());
						tmpEntity.setA0184_2(umsUserErInfoVo.getA0184());
						break;
					case 2:
						tmpEntity.setA0000_3(umsUserErInfoVo.getA0000());
						tmpEntity.setA0101_3(umsUserErInfoVo.getA0101());
						tmpEntity.setA0184_3(umsUserErInfoVo.getA0184());
						break;
					case 3:
						tmpEntity.setA0000_4(umsUserErInfoVo.getA0000());
						tmpEntity.setA0101_4(umsUserErInfoVo.getA0101());
						tmpEntity.setA0184_4(umsUserErInfoVo.getA0184());
						break;
					case 4:
						tmpEntity.setA0000_5(umsUserErInfoVo.getA0000());
						tmpEntity.setA0101_5(umsUserErInfoVo.getA0101());
						tmpEntity.setA0184_5(umsUserErInfoVo.getA0184());
						break;
					case 5:
						tmpEntity.setA0000_6(umsUserErInfoVo.getA0000());
						tmpEntity.setA0101_6(umsUserErInfoVo.getA0101());
						tmpEntity.setA0184_6(umsUserErInfoVo.getA0184());
						break;
				}

			}
		}catch (Exception e){
			return Lists.newArrayList();
		}

		return objects1;
	}
	public List<UmsUserErInfoVo> buildReport(String dsName,String datasetName,Map<String,Object> parameters){
		log.info(datasetName+dsName+parameters);
		List<UmsUserErInfoVo> objects = Lists.newArrayList();
		for (int i = 0; i < 100; i++) {
			UmsUserErInfoVo umsUserErInfoVo = new UmsUserErInfoVo();
			umsUserErInfoVo.setA0000("a5c8a5e8-df2b-4706-bea4-08d0939410e3"+i);
			umsUserErInfoVo.setA0101("张三"+i);
			umsUserErInfoVo.setA0184("4107841454414879485"+i);
			umsUserErInfoVo.setBase64A0000(Base64.encode("张三@a5c8a5e8-df2b-4706-bea4-08d0939410e3"));
			objects.add(umsUserErInfoVo);
		}
		return objects;
	}

	public Boolean upDateBuCode(List<A01Entity> a01Entities) {
		tmpBu=a01Entities;
		return true;
	}
}
